<!--  -->
<template>
  <div>
    <el-card shadow="hover">
        <div class="button_form">
            <el-button-group>
                <el-button type="primary" icon="el-icon-circle-plus-outline" @click="addTbSpecDialogVisible=true">新增规格</el-button>
                <el-button type="primary" icon="el-icon-edit" @click="openAddTbSpecOpDialog">新增规格选项</el-button>
                <el-button type="primary" icon="el-icon-remove-outline" @click="deleteTbSpec">删除规格</el-button>
                <el-button type="primary" icon="el-icon-refresh" @click="selectAllTbSpecByPage">刷新</el-button>
            </el-button-group>
            
            <el-form :inline="true" :model="selectForm" class="demo-form-inline" >
                <el-form-item label="规格名称">
                    <el-input v-model="selectForm.specName" placeholder="规格名称" clearable @clear="selectAllTbSpecByPage"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="selectAllTbSpecByCond" icon="el-icon-search">查询</el-button>
                </el-form-item>
            </el-form>
        </div>
        <el-table
            ref="mulTbSpecTable"
            :data="this.tbSpecList"
            style="width: 100%"
            @selection-change="handleSelectionChange"
            :header-cell-style="{color:'#000'}">
            <el-table-column
                type="selection"
                width="80">
            </el-table-column>
            <el-table-column
                label="规格ID"
                prop="id">
            </el-table-column>
            <el-table-column
                label="规格名称"
                prop="specName">
            </el-table-column>
            <el-table-column
                label="操作">
                <template slot-scope="scope">
                    <el-button
                    size="mini"
                    type="primary"
                    plain
                    @click="handleEdit(scope.$index, scope.row)">修改</el-button>
                </template>
            </el-table-column>
        </el-table>
    </el-card>
    <!--    分页-->
    <el-pagination
        align="right"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
    <!-- 添加规格对话框 -->
    <el-dialog
      title="添加规格信息"
      :visible.sync="addTbSpecDialogVisible"
      width="40%"
      center
      :close-on-click-modal="false">
      <el-form :model="addTbSpecMsgForm"  ref="addTbSpecMsgForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="规格ID" prop="specId" required>
          <el-input v-model="addTbSpecMsgForm.specId" clearable></el-input>
        </el-form-item>        
        <el-form-item label="规格名称" prop="specName" required>
          <el-input v-model="addTbSpecMsgForm.specName" placeholder="规格名称" clearable></el-input>
        </el-form-item>
        <!-- <el-form-item label="规格选项" prop="optionName" required>
          <el-input v-model="addTbSpecMsgForm.optionName" placeholder="规格选项" clearable></el-input>
        </el-form-item> -->
        <!-- <el-form-item label="规格选项">
            <el-checkbox :indeterminate="isIndeterminate" 
              v-model="checkAll" 
              @change="handleCheckAllChange"
            >全选</el-checkbox>
            <div style="margin: 15px 0;"></div>
            <el-checkbox-group v-model="checkedTbSpecOP" @change="handleCheckedTbSpecOPChange">
                <el-checkbox v-for="option in tbSpecOPs " :label="option" :key="option">{{option}}</el-checkbox>
            </el-checkbox-group>
        </el-form-item> -->
        <el-form-item style="margin: 0 auto;">
          <el-button type="primary" @click="addTbSpec" plain>立即添加</el-button>
          <el-button type="warning" @click="resetForm('addTbSpecMsgForm')" plain >重置</el-button>
          <el-button @click="addTbSpecDialogVisible = false">取 消</el-button>
        </el-form-item>
      </el-form>
      <!-- <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="centerDialogVisible = false">确 定</el-button>
      </span> -->
    </el-dialog>
    <!-- 添加规格选项对话框 -->
    <el-dialog
      title="添加规格选项信息"
      :visible.sync="addTbSpecOpDialogVisible"
      width="40%"
      center
      :close-on-click-modal="false">
      <el-form :model="addTbSpecOpMsgForm" ref="addTbSpecOpMsgForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="规格名称" required>
            <el-select v-model="selectedSpecName" placeholder="请选择规格名称">
                <el-option
                    v-for="option in specNameOptions"
                    :key="option.value"
                    :label="option.label"
                    :value="option.value">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="规格选项" prop="optionName" required>
          <el-input v-model="addTbSpecOpMsgForm.optionName" placeholder="规格选项" clearable></el-input>
        </el-form-item>
        <el-form-item style="margin: 0 auto;">
          <el-button type="primary" @click="addTbSpecOp" plain>立即添加</el-button>
          <el-button type="warning" @click="resetForm('addTbSpecOpMsgForm')" plain >重置</el-button>
          <el-button @click="addTbSpecOpDialogVisible = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import tbSpecApi from '@/api/tbSpecApi'
export default {
  name: 'TbSpecList',
  data () {
    return {
        page: 1,
        pageSize: 5,
        allTbSpecList:[],
        tbSpecList:[],
        total:0,
        selectForm:{
            specName:'',
        },
        addTbSpecDialogVisible:false,
        addTbSpecOpDialogVisible:false,
        addTbSpecMsgForm:{
            specId:0,
            specName:'',
            // optionName:''
        },
        addTbSpecOpMsgForm:{
            specId:0,
            specName:'',
            optionName:''
        },
        selectedSpecName:'',
        specNameOptions: [],

        deletedData:[],
        
        // checkAll: false,
        // checkedTbSpecOP: [],
        // tbSpecOPs: [],
        // isIndeterminate: true
    }
  },
  created() {},
  mounted() {
    // this.selectAll()
    this.selectAllTbSpecByPage()
  },
  methods:{
    //查询全部
    async selectAll(){
       let res= await tbSpecApi.selectAll()
    //    console.log("tbSpecList")
       console.log(res)
    },
    //简单分页查询
    async selectAllTbSpecByPage(){
        let params={
            page:this.page,
            pageSize:this.pageSize
        }
        let res=await tbSpecApi.selectAllTbSpecByPage(params);
        console.log("tbSpecList")
        this.tbSpecList=res.data.rows;
        this.total=res.data.total;
        console.log(res.data)
    },
    //条件查询带分页
    async selectAllTbSpecByCond(){
        let res=await tbSpecApi.selectAllTbSpecByCond(this.page,this.pageSize,this.selectForm);
        console.log("CondTbSpecList")
        this.tbSpecList=res.data.rows;
        this.total=res.data.total;
        console.log(res.data)
    },
    //添加规格信息
    async addTbSpec(){
        console.log(this.addTbSpecMsgForm);
        let res=await tbSpecApi.addTbSpec(this.addTbSpecMsgForm);
        // console.log(res);
        if (res.data.code==200) {
            this.$message({
                type:"success",
                message:res.data.msg
            })
        }
        this.addTbSpecDialogVisible=false;
        this.resetForm("addTbSpecMsgForm") 
        this.selectAllTbSpecByPage;
    },
    //打开添加规格选项信息对话框
    async openAddTbSpecOpDialog(){
        this.addTbSpecOpDialogVisible=true;
        let res=await tbSpecApi.selectAll();
        this.allTbSpecList=res.data
        for (let i = 0; i < res.data.length; i++) {
            this.specNameOptions.push({label:res.data[i].specName,value:res.data[i].specName})
        }
    },
    //添加规格选项信息
    async addTbSpecOp(){
        for(let i=0;i<this.allTbSpecList.length;i++){
            if(this.allTbSpecList[i].specName==this.selectedSpecName){
                this.addTbSpecOpMsgForm.specId=this.allTbSpecList[i].id;
            }
        }
        this.addTbSpecOpMsgForm.specName=this.selectedSpecName;
        let res= await tbSpecApi.addTbSpecOp(this.addTbSpecOpMsgForm)
        console.log(res)
        if(res.data.code==200){
            this.$message({
                type:'success',
                message:res.data.msg
            })
        }else{
            this.$message({
                type:'error',
                message:res.data.msg
            })
        }
        this.addTbSpecOpDialogVisible=false;
        this.resetForm("addTbSpecOpMsgForm") 
        this.selectAllTbSpecByPage;
    },
    //删除数据
    //获取选中删除的表项数据
    handleSelectionChange(selection){
        for(let i=0;i<selection.length;i++){
            this.deletedData.push(selection[i].id);
        }
        // console.log(selection);
    },
    async deleteTbSpec(){
        if(this.deletedData.length==0){
            this.$message({
                type: 'warning',
                message: '请选择要删除的数据!'
            });
            return;
        }
        let res=await tbSpecApi.deleteTbSpec(this.deletedData);
        if(res.data.code==200){
            this.$message({
                type:"success",
                message:res.data.msg
            })
        }
        this.selectAllTbSpecByPage();
    },
    //获取
    getTbSpecOptions(){

    },
    //处理规格选项多选
    handleCheckAllChange(val) {
        this.checkedCities = val ? this.tbSpecOPs : [];
        this.isIndeterminate = false;
      },
    handleCheckedTbSpecOPChange(value) {
        let checkedCount = value.length;
        this.checkAll = checkedCount === this.tbSpecOPs.length;
        this.isIndeterminate = checkedCount > 0 && checkedCount < this.tbSpecOPs.length;
    },


    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.selectAllTbSpecByPage();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.page = val;
      this.selectAllTbSpecByPage();
    },
    //表格项编辑函数
    handleEdit(index, row) {
      // console.log(index, row);
    },
    //重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

  }
}
</script>

<style  scoped>
    .el-card{
        width:90%;
        margin: 0 auto;
    }
    .el-pagination{
        margin : 20px;
        margin-right: 80px;
    }
    .demo-form-inline{
        width: 40%;
    }
    .button_form{
        display: flex;
        justify-content:space-between
    }
</style>
